package com.oil.petro.eshop.dao;

import com.oil.petro.eshop.entity.Account;
import com.oil.petro.eshop.entity.Order;
import com.oil.petro.eshop.entity.Product;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository("OrderDAO")
public class OrderDAOImpl implements OrderDAO {

    @Autowired
    private SessionFactory sessionfactory;

    @Override
    public Order get(int id) {
        return (Order) sessionfactory.getCurrentSession().get(Order.class, id);
    }

    @Override
    public void delete(Order order) {
        sessionfactory.getCurrentSession().delete(order);
    }

    @Override
    public void delete(int orderId) {
        Order order = get(orderId);
        
        sessionfactory.getCurrentSession().delete(order);
    }
    
    @Override
    public int save(Order order) {
        return (int) sessionfactory.getCurrentSession().save(order);
    }

    public List<Product> getList(int accountId) {
        Query query = sessionfactory.getCurrentSession().createQuery("from Order where account");

        return query.list();
    }

    @Override
    public void update(Order order) {
        sessionfactory.getCurrentSession().saveOrUpdate(order);
    }

    @Override
    public boolean isEmpty(int accountId, int productId) {

       Order order = (Order)sessionfactory.getCurrentSession().createQuery("from Order where product_id=" + productId + " and account_id=" + accountId).uniqueResult();

        if (order == null) {
            return true;

        } else {
            return false;
        }
    }

   
    @Override
    public Order get(int accountId, int productId) {
        Order order = (Order) sessionfactory.getCurrentSession().createQuery("from Order where product_id=" + productId + " and account_id=" + accountId).uniqueResult();

        return order;
    }

    @Override
    public List<Order> getOrderedProducts(int accountId) {

        return sessionfactory.getCurrentSession().createQuery("from Order where account_id=" + accountId).list();

    }
    
     @Override
    public List<Order> getAccountsForProduct(int productId) {

        return sessionfactory.getCurrentSession().createQuery("from Order where product_id=" + productId).list();

    }

    @Override
    public void addOrder(int accountId, int productId, int quantity) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public List<Product> getOrders() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    
}
